<?php


namespace app\backend\controller\v1;
use app\BaseController;
use think\facade\Filesystem;

class Upload extends BaseController
{
    //上传图片
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file();
        if (empty($file))
            return errMsg(400,'请上传文件!');
        $k=array_keys($file);
        $saveName = Filesystem::disk('upload')->putFile('/upload', $file[$k[0]]);
        if (!empty($saveName)) {
            $saveName = str_replace('\\', "/", $saveName);
            return sucessMsg(200, '上传成功', $saveName);
        }
    }

    //多图上传
    public function uploads()
    {
        $files = $_FILES;
        if(!$files){
            return errMsg(400,'请上传图片！');
        }
        $imageArr = Array();
        foreach ($files as $file) {
            $imageName = $file['name'];
            dd($imageName);
            //后缀名
            $ext = strtolower(substr(strrchr($imageName,'.'),1));
            //保存文件名
            $fileName = uniqid();
            $tmp = $file['tmp_name'];
            //保存 = 路径 + 文件名 + 后缀名
            $imageSavePath = '../public/uploads/images/'.$fileName.'.'.$ext;
            $info = move_uploaded_file($tmp, $imageSavePath);
            if ($info) {
                $path = "/uploads/images/".$fileName.'.'.$ext;
                array_push($imageArr, $path);
            }else{
                return errMsg(400,'上传失败！');
            }
        }
        //最终生成的字符串路径
        $imagePathStr = implode(',', $imageArr);
        return sucessMsg(200,'上传成功！',$imagePathStr);
    }

}